// 
//  DatabaseBuilder.cs
//  
//  Author:
//       Ray Shaw <demogodyou@gmail.com>
// 
//  Copyright (c) 2009 Ray Shaw(xiaolei)
// 
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// 
//------------------------------------------------------------------------------
// <auto-generated>
//     此代码由工具生成。
//     运行时版本:2.0.50727.3053
//
//     对此文件的更改可能会导致不正确的行为，并且如果
//     重新生成代码，这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Data;
using System.IO;
using SharpNote.Core.Data;

namespace SharpNote.Core.Data
{
	/// <summary>
	/// Execute sql script in file: build_db.sql.
	/// </summary>
	public class DatabaseBuilder
	{
		private readonly string _sqlFileName = "build_db.sql";
		private readonly string _folderName = "Data";
		private string _connectionString = string.Empty;

		public DatabaseBuilder (string connectionString)
		{
			_connectionString = connectionString;
		}

		/// <summary>
		/// Execute sql script in file: build_db.sql.
		/// </summary>
		/// <returns>
		/// A <see cref="System.Boolean"/>
		/// </returns>
		public bool Build ()
		{
			try {
				string sqlFileName = System.IO.Path.Combine (System.IO.Path.Combine (AppDomain.CurrentDomain.BaseDirectory, _folderName), _sqlFileName);
				if (System.IO.File.Exists (sqlFileName)) 
				{
					using (StreamReader reader = new StreamReader (sqlFileName)) 
					{
						DataAccessService das = new DataAccessService (_connectionString);
						string sql = reader.ReadToEnd ();
						if (!string.IsNullOrEmpty (sql)) 
						{
							das.ExecuteNonQuery (sql);
						}

						return true;
					}
				}
			} catch (Exception ex) {
#if DEBUG
	throw ex;			
#endif
				return false;
			}

			return false;
		}
	}
}
